<?php
set_time_limit(15000);
	mysql_connect( "212.72.155.176", "root", "Gl-1114");
    mysql_select_db("asteriskcdrdb");

$q=mysql_query("SELECT
						realizations.id
						FROM
						realizations");

while ($row = mysql_fetch_array($q)) {
	$sum=mysql_fetch_array(mysql_query("	SELECT 	SUM(Sum)
						FROM 	nomenclature
						WHERE 	realizations_id=$row[0]"));
	$a=mysql_query("UPDATE realizations
								SET realizations.Sum = $sum[0]
				WHERE 				id				 = $row[0] ");

}